<div class="row">
    <div class="colxl-12 col-lg-12 col-md-12 col-sm-12 col-12">
        <div class="page-title-wrapper">
            <div class="page-title-box">
                <h4 class="page-title bold">友链管理</h4>
            </div>
            <div class="breadcrumb-list">
                <ul>
                    <li class="breadcrumb-link"><a th:href="@{admin}"><i class="fas fa-home mr-2"></i>首页</a></li>
                    <li class="breadcrumb-link active">网站管理</li>
                    <li class="breadcrumb-link active">友链管理</li>
                </ul>
            </div>
        </div>
    </div>
</div>
<!-- Main content -->
<section class="content">
    <div class="card card-info card-header">
        <form id="searchForm" class="needs-validation form-inline" novalidate="">
            <div class="form-group">
                <label for="name">站点名称:</label>
                <input type="text" class="form-control m-2" id="name" required="">
            </div>
            <div class="form-group">
                <label for="url">站点链接:</label>
                <input type="text" class="form-control m-2" id="url" required="">
            </div>
            <div class="form-group">
                <label for="status">状态:</label>
                <select id="status" class="form-control m-2" required="">
                    <option selected disabled>选择状态</option>
                    <option value="1">启用</option>
                    <option value="0">禁用</option>
                </select>
            </div>
            <div class="form-group">
                <button class="btn btn-primary ml-3" type="submit">查询</button>
                <button class="btn btn-light ml-2" type="reset">重置</button>
            </div>
        </form>
    </div>
    <div class="card table-card card-body">
        <div id="toolbar" class="btn-group">
                <button id="btn_add" type="button" class="btn btn-primary">
                    <span class="fa fa-plus" aria-hidden="true"></span>新增
                </button>
                <button id="btn_batch_delete" type="button" class="btn btn-danger">
                    <span class="fa fa-trash" aria-hidden="true"></span>批量删除
                </button>
        </div>
        <table class="table-sm" id="table"></table>
    </div>
</section>

<!-- 表单模态框 -->
<div class="modal fade" id="formModal" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="formTitle"></h5>
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div id="formContent"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="saveOrUpdateBtn">保存
                </button>
            </div>
        </div>
    </div>
</div>
<script>
    $(function () {
        window.operateEvents = {
            // 点击编辑
            'click .btn_edit': function (e, value, row, index) {
                editRow(row.id)
            },
            // 点击删除
            'click .btn_del': function (e, value, row, index) {
                deleteRow(row.id)
            },
            // 点击预览
            'click .btn_view': function (e, value, row, index) {
                previewImg(value)
            }
        }
        var columns = [
            {checkbox: true},
            {
                field: 'name',
                title: '站点名称',
                align: "center"
            }, {
                field: 'url',
                title: '站点链接',
                align: "center",
                formatter: function (value, row, index) {
                    return '<a href="' + value + '" target="_blank">'+value+'</a>'
                }
            }, {
                field: 'description',
                title: '站点描述',
                align: "center",
            }, {
                field: 'email',
                title: '站长邮箱',
                align : "center",
            },{
                field: 'qq',
                title: '站长QQ',
                align : "center",
            },{
                field: 'status',
                title: '状态',
                align : "center",
                formatter: typeSwitch
            }, {
                field: 'operation',
                title: '操作',
                align: "center",
                width: 200,
                events: operateEvents,
                formatter: operationFormatter
            }];
        const onLoadSuccess = function () {	//在成功加载远程数据时触发
            $("[name='switch']").bootstrapSwitch({
                onText: "启用",      // 设置ON文本
                offText: "禁用",    // 设置OFF文本
                onColor: "success",// 设置ON文本颜色(info/success/warning/danger/primary)
                offColor: "warning",  // 设置OFF文本颜色 (info/success/warning/danger/primary)
                size: "small",    // 设置控件大小,从小到大  (mini/small/normal/large)
                // 当开关状态改变时触发
                onSwitchChange: function (event, state) {
                    const status = state ? 1 : 0;
                    const params = {id: this.value, status: status};
                    const str = jQuery.param(params);
                    Core.postAjax('/link/edit', str, function (data) {
                        if (data.status === 200) {
                            refreshTable();
                        }
                        layer.msg(data.msg);
                    })
                }
            });
        };

        //表格中开关控件
        function typeSwitch(value, row, index){
            if (value) {
                return "<input value=" + row.id + " name='switch' type='checkbox' checked />";
            } else {
                return "<input value=" + row.id + " name='switch' type='checkbox' />";
            }
        }
        function operationFormatter(value, row, index) {
            var edit = '<button class="effect-btn btn btn-info mt-2 mr-2 icon-lg btn_edit"><i class="fas fa-edit"></i></button>';
            var del ='<button class="effect-btn btn btn-danger mt-2 mr-2 icon-lg btn_del"><i class="fas fa-trash-alt"></i></button>';
            return edit + del;
        }

        var options = {
            id: "#table",
            url: '/link/list',
            columns: columns,
            toolbar: '#toolbar',
            showRefresh: true,
            onLoadSuccess:onLoadSuccess,
            queryParams: queryParams
        }
        Core.initTable(options);

        function refreshTable() {
            Core.refreshTable("#table")
        }

        // 查询参数
        function queryParams(params) {
            return {
                pageNumber: params.pageNumber,
                pageSize: params.pageSize,
                name: $("#name").val(),
                url: $("#url").val(),
                status: $("#status").val(),
            };
        }

        // 查询
        $('#searchForm').submit(function (event) {
            event.preventDefault();
            event.stopPropagation();
            refreshTable();
        })

        // 新增
        $('#btn_add').on('click', function () {
            Core.load("#formContent", "/link/add", function () {
                $('#formTitle').text('新增')
                $("#formModal").modal('show');
            });
        })
        //只是在点击后初始化的时候生成
        $("#formModal").on("shown.bs.modal", function () {
            // 初始化Web Uploader
            var uploader = WebUploader.create({
                // 选完文件后，是否自动上传。
                auto: true,
                // swf文件路径
                swf: '/admin1/img/Uploader.swf',
                // 文件接收服务端。
                server: '[[@{/attachment/upload}]]',
                // 选择文件的按钮。可选。
                // 内部根据当前运行是创建，可能是input元素，也可能是flash.
                pick: '#filePicker',
                // 只允许选择图片文件。
                accept: {
                    title: 'Images',
                    extensions: 'gif,jpg,jpeg,bmp,png,ico',
                    mimeTypes: 'image/*'
                }
            });
            // 文件上传成功，给item添加成功class, 用样式标记上传成功。
            var  $list=$('#fileList');
            uploader.on( 'uploadSuccess', function( file,data ) {
                var $li = $(
                    '<li class="content-img-list-item"><img src="' + data.url + '" >' +
                    '<div ><button class="btn-outline secondary-outline mt-2 mr-2 icon-sm"><i class="fas fa-trash-alt"></i></button></div></i>'
                );
                $list.append( $li );
                $("#img").val(data.url);
                $('.content-img .file').hide();
            });
            // 文件上传失败，显示上传出错。
            uploader.on( 'uploadError', function( file ) {
                console.log('上传失败');
            });
            // 鼠标经过显示删除按钮
            $('.content-img-list').on('mouseover', '.content-img-list-item', function() {
                $(this).children('div').removeClass('hide');
            });
            // 鼠标离开隐藏删除按钮
            $('.content-img-list').on('mouseleave', '.content-img-list-item', function() {
                 $(this).children('div').addClass('hide');
            });
            // 单个图片删除
            $(".content-img-list").on("click", '.content-img-list-item button .fa-trash-alt', function() {
                $(this).parent().parent().parent().remove();
                $('.content-img .file').show();
            });
        })

        // 编辑
        function editRow(id) {
            Core.load("#formContent", "/link/edit?id=" + id, function () {
                $('#formTitle').text('编辑')
                $("#formModal").modal('show');
            });
        }

        // 删除
        function deleteRow(id) {
            Core.confirm("确定删除？", function () {
                Core.postAjax("/link/delete", {"id": id}, function (data) {
                    if (data.status === 200) {
                        refreshTable();
                    }
                    layer.msg(data.msg);
                })
            })
        }

        // 批量删除
        $("#btn_batch_delete").on('click', function () {
            var checkedRows = Core.selectMutiData("#table");
            if (checkedRows) {
                Core.confirm("确定删除选中的" + checkedRows.length + "条记录？", function () {
                    var ids = [];
                    $.each(checkedRows, function (i, item) {
                        ids.push(item.id);
                    })
                    var jsonStr = JSON.stringify(ids);
                    Core.postAjax("/link/batch/delete", jsonStr, function (data) {
                        if (data.status === 200) {
                            refreshTable();
                        }
                        layer.msg(data.msg);
                    },function (err) {
                        layer.msg(err)
                    },"application/json; charset=UTF-8")
                })
            }
        })

        // 保存或更新
        $('#saveOrUpdateBtn').on('click', function () {
            var $f = $('#linkForm');
            var valid = $f[0].checkValidity();
            var url = $f.find("input[name='id']").val() ? "/link/edit" : "/link/add"
            if (valid) {
                Core.mask("#saveOrUpdateBtn");
                Core.postAjax(url, $f.serialize(), function (data) {
                    Core.unmask("#saveOrUpdateBtn");
                    if (data.status === 200) {
                        $("#formModal").modal("hide");
                        refreshTable();
                    }
                    layer.msg(data.msg);
                }, function () {
                    Core.unmask("#saveOrUpdateBtn");
                })
            }
            $f.addClass("was-validated")
        })

    });

    function previewImg(src) {
        layer.open({
            type: 1,
            title: false,
            closeBtn: 0,
            area: '45%',
            skin: 'layui-layer-nobg', //没有背景色
            shadeClose: true,
            content: '<img style="width: 100%" alt="link" src="' + src + '">'
        });
    }

</script>